package biz

import (
	"fmt"
	"github.com/yeahyf/go_base/immut"
	"google.golang.org/protobuf/proto"
)

type ReqMessage interface {
	RespMessage
	CheckID() int
}

type RespMessage interface {
	proto.Message
	DebugString() string
}

func pageInfoCheck(info *PageInfo) int {
	if info == nil {
		return ParamCheckErrCode
	}
	if info.PageNumber <= 0 {
		info.PageNumber = 1
	}
	if info.PageSize <= 0 || info.PageSize >= 500 {
		info.PageSize = 500
	}
	return SuccessCode
}

// =======================  User ================

func (resp *User) DebugString() string {
	return fmt.Sprintf("%+v", resp)
}

// =======================  UserAddReq ================

func (req *UserAddReq) DebugString() string {
	return fmt.Sprintf("%+v", req)
}

func (req *UserAddReq) CheckID() int {
	return SuccessCode
}

// =======================  UserListReq ================

func (req *UserListReq) DebugString() string {
	return fmt.Sprintf("%+v", req)
}

func (req *UserListReq) CheckID() int {
	code := pageInfoCheck(req.PageInfo)
	if code != SuccessCode {
		return code
	}
	return SuccessCode
}

func (resp *UserListResp) DebugString() string {
	return fmt.Sprintf("%+v", resp)
}

// =======================  UserLoginReq ================

func (req *UserLoginReq) DebugString() string {
	return fmt.Sprintf("%+v", req)
}

func (req *UserLoginReq) CheckID() int {
	if req.Account == immut.Blank || req.Password == immut.Blank {
		return ParamCheckErrCode
	}
	return SuccessCode
}

func (resp *UserLoginResp) DebugString() string {
	return fmt.Sprintf("%+v", resp)
}

// =======================  UserUpdateReq ================

func (req *UserUpdateReq) DebugString() string {
	return fmt.Sprintf("%+v", req)
}

func (req *UserUpdateReq) CheckID() int {
	if req.User == nil || req.User.Account == immut.Blank {
		return ParamCheckErrCode
	}
	return SuccessCode
}

// =======================  UserDelReq ================

func (req *UserDelReq) DebugString() string {
	return fmt.Sprintf("%+v", req)
}

func (req *UserDelReq) CheckID() int {
	if req.Account == immut.Blank {
		return ParamCheckErrCode
	}
	return SuccessCode
}
